import {
	observer
} from 'mobx-react';
import {
	WeaTableNew
} from 'comsMobx';
import {
	WeaPopoverHrm
} from 'ecCom';
import * as PublicFunc from '../../util/pulic-func';
import {
	jumpToHrmCard,
	jumpToHrmSubCompany,
	jumpToHrmDept,
	jumpToHrmPost
} from '../../util/pulic-func'
const WeaTable = WeaTableNew.WeaTable;

@observer
export default class Table extends React.Component {
	constructor(props) {
		super(props);
	}

	reRenderColumns = (columns) => {
		columns.forEach(c => {
			c.render = function(text, record) {
				let valueSpan = record[c.dataIndex + "span"] !== undefined ? record[c.dataIndex + "span"] : record[c.dataIndex];
				return <span dangerouslySetInnerHTML={{__html: valueSpan}}></span>
			}
			// if (c.dataIndex == 'lastname') {
			// 	c.render = function(text, record) {
			// 		return <a onClick={()=>jumpToHrmCard(record.id)}>{record.lastname}</a>
			// 	}
			// } else if (c.dataIndex == 'managerid') {
			// 	c.render = function(text, record) {
			// 		return <a onClick={()=>jumpToHrmCard(record.managerid)} dangerouslySetInnerHTML={{__html: record.manageridspan}}></a>
			// 	}
			// } else if (c.dataIndex == 'subcompanyid1') {
			// 	c.render = function(text, record) {
			// 		return <a onClick={()=>jumpToHrmSubCompany(record.subcompanyid1)} dangerouslySetInnerHTML={{__html: record.subcompanyid1span}}></a>
			// 	}
			// } else if (c.dataIndex == 'departmentid') {
			// 	c.render = function(text, record) {
			// 		return <a onClick={()=>jumpToHrmDept(record.departmentid)} dangerouslySetInnerHTML={{__html: record.departmentidspan}}></a>
			// 	}
			// } else if (c.dataIndex == 'jobtitle') {
			// 	c.render = function(text, record) {
			// 		return <a onClick={()=>jumpToHrmPost(record.jobtitle)} dangerouslySetInnerHTML={{__html: record.jobtitlespan}}></a>
			// 	}
			// } else {
			// 	c.render = function(text, record) {
			// 		let valueSpan = record[c.dataIndex + "span"] !== undefined ? record[c.dataIndex + "span"] : record[c.dataIndex];
			// 		return <span dangerouslySetInnerHTML={{__html: valueSpan}}></span>
			// 	}
			// }
		})
		return columns;
	}

	onOperatesClick = (record, index, operate) => {
		const fn = operate.href ? operate.href.split(':')[1].split('(')[0] : '';
		const id = record.id ? record.id : '';
		PublicFunc[fn](id);
	}

	render() {
		const {
			store
		} = this.props, {
			TABLE,
			table
		} = store;

		return (
			<WeaTable 
				{...TABLE}
				{...table}
				getColumns={columns => this.reRenderColumns(columns)}
				onOperatesClick={(record, index, operate) => this.onOperatesClick(record,index, operate)}
			/>
		);
	}
}